<?php

namespace App\Console\Commands;

use App\Store\Models\Financial\Goods\GoodsWarning as GoodsGoodsWarning;
use App\Store\Models\Goods\GoodsStockList;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class GoodsWarning extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:GoodsWarning';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取库存预警列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('获取库存预警列表开始');
        $this->getwarninglist();
        Log::info('获取库存预警列表结束');
        return 0;
    }
    protected function getwarninglist(){
        $list=GoodsGoodsWarning::where(['is_del'=>1])->get()->toArray();
        $nowarning=[];
        $warning=[];
        foreach($list as $val){
            $stock_num=GoodsStockList::where(['cp_id'=>$val['cp_id'],'store_id'=>$val['store_id'],'goods_category_id'=>$val['goods_category_id'],'goods_style_id'=>$val['goods_style_id'],'is_del'=>1])
            ->sum('count');
            if($stock_num>$val['warning_stock']){//不预警
                $val['warning_at']>0 && $nowarning[]=$val['id'];
            }else{
                $val['warning_at']<=0 && $warning[]=$val['id'];
            }
        }
        GoodsGoodsWarning::where(['is_del'=>1])->whereIn('id',$warning)->update(['warning_at'=>time()]);
        GoodsGoodsWarning::where(['is_del'=>1])->whereIn('id',$nowarning)->update(['warning_at'=>0]);
    }
}
